package com.gxa.modules.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.modules.sys.entity.RoleManagement;
import com.gxa.modules.sys.entity.RoleMenu;
import com.gxa.modules.sys.mapper.RoleManagementMapper;
import com.gxa.modules.sys.service.RoleManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Component
public class RoleManagementServiceImpl implements RoleManagementService {
    @Autowired
    private RoleManagementMapper roleManagementMapper;

    @Override
    public PageInfo<RoleManagement> queryRoleManagement(Map<String, Object> params) {
        String page1=(String) params.get("page");
        String limit1=(String)params.get("limit");
        Integer page=Integer.valueOf(page1);
        Integer limit=Integer.valueOf(limit1);

        //条件查询
        String roleName = (String) params.get("roleName");

        PageHelper.startPage(page,limit);

        List<RoleManagement> roleManagements = this.roleManagementMapper.queryRoleManagement(roleName);

        PageInfo<RoleManagement> pageInfo = new PageInfo<>(roleManagements);
        return pageInfo;

    }

    @Override
    public void addRoleManagement(RoleManagement roleManagement) {
        this.roleManagementMapper.addRoleManagement(roleManagement);
        //加入角色回显的主键id
        int reloId = roleManagement.getId();
        System.out.println("reloId---------->"+reloId);

        System.out.println("roleManagement.getRoleMenu().getMenuId()----->"+roleManagement.getRoleMenu().getMenuIds()[0]);
        System.out.println("roleManagement.getRoleMenu().getMenuId()----->"+roleManagement.getRoleMenu().getMenuIds()[1]);
        System.out.println("roleManagement.getRoleMenu().getRountingId()----->"+roleManagement.getRoleMenu().getRountingIds()[0]);
        System.out.println("roleManagement.getRoleMenu().getRountingId()----->"+roleManagement.getRoleMenu().getRountingIds()[1]);

        //1 创建list集合，用于封装添加数据
        for (int i=0;i<roleManagement.getRoleMenu().getMenuIds().length;i++){
        RoleMenu roleMenu = new RoleMenu(roleManagement.getId(),roleManagement.getRoleMenu().getMenuIds()[i]);
        System.out.println("roleMenu---Perms-->"+roleMenu.getRoleId());
        System.out.println("roleMenu--Perms--->"+roleMenu.getMenuId());
        this.roleManagementMapper.addPerms(roleMenu);
        }
        //1 创建list集合，用于封装添加数据
        for (int i=0;i<roleManagement.getRoleMenu().getRountingIds().length;i++){
            System.out.println("roleMenu rounting----->"+reloId);
            System.out.println("roleMenu  rounting----->"+roleManagement.getRoleMenu().getRountingIds()[i]);
            this.roleManagementMapper.addRountingIds(reloId,roleManagement.getRoleMenu().getRountingIds()[i]);
        }
    }

    @Override
    public void deleteRoleManagement(Integer id) {
        this.roleManagementMapper.deleteRoleManagement(id);
        this.roleManagementMapper.deleteRoleMenu(id);
        this.roleManagementMapper.deleteRoleRounting(id);
    }
}
